﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.UI
{
    public class EventBus
    {
        private static EventBus s_instance = null;

        public static EventBus Instance
        {
            get
            {
                if (s_instance == null)
                {
                    s_instance = new EventBus();
                }
                return s_instance;
            }
        }

        private List<IPartInstanceSelectedEventReceiver> _partInstanceSelectedEventReceiverList;
        private List<IPartMetadataSelectedEventReceiver> _partMetadataSelectedEventReceiverList;
        private List<ISolutionItemChangedEventReceiver> _solutionItemChangedEventReceiverList;
        private List<IPartLibItemSelectedEventReceiver> _partLibItemSelectedEventReceiverList;
        
        public EventBus()
        {
            _partInstanceSelectedEventReceiverList = new List<IPartInstanceSelectedEventReceiver>();
            _partMetadataSelectedEventReceiverList = new List<IPartMetadataSelectedEventReceiver>();
            _solutionItemChangedEventReceiverList = new List<ISolutionItemChangedEventReceiver>();
            _partLibItemSelectedEventReceiverList = new List<IPartLibItemSelectedEventReceiver>();
        }

        public void Register<T>(T receiver)
        {
            if (typeof(T) == typeof(IPartLibItemSelectedEventReceiver))
            {
                _partLibItemSelectedEventReceiverList.Add((IPartLibItemSelectedEventReceiver)receiver);
            }
            else if (typeof(T) == typeof(ISolutionItemChangedEventReceiver))
            {
                _solutionItemChangedEventReceiverList.Add((ISolutionItemChangedEventReceiver)receiver);
            }
            else if (typeof(T) == typeof(IPartInstanceSelectedEventReceiver))
            {
                _partInstanceSelectedEventReceiverList.Add((IPartInstanceSelectedEventReceiver)receiver);
            }
        }

        /// <summary>
        /// 创建方案
        /// </summary>
        /// <param name="solutionMetadata"></param>
        public void CreateSolution(Metadata.Solution solutionMetadata)
        {
            for (int index = 0; index < _solutionItemChangedEventReceiverList.Count; index++)
            {
                _solutionItemChangedEventReceiverList[index].OnSolutionCreated(solutionMetadata);
            }
        }

        public void CloseSolution(Metadata.Solution solutionMetadata)
        {
        }

        public void CreateScene(Metadata.Scene sceneMetadata)
        {
            for (int index = 0; index < _solutionItemChangedEventReceiverList.Count; index++)
            {
                _solutionItemChangedEventReceiverList[index].OnSceneCreated(sceneMetadata);
            }
        }

        public void RemoveScene(Metadata.Scene sceneMetadata)
        {
        }

        ///// <summary>
        ///// 创建区域 
        ///// </summary>
        ///// <param name="areaMetadata"></param>
        //public void CreateArea(Metadata.Area areaMetadata)
        //{
        //    for (int index = 0; index < _solutionItemChangedEventReceiverList.Count; index++)
        //    {
        //        _solutionItemChangedEventReceiverList[index].OnAreaCreated(areaMetadata);
        //    }
        //}

        ///// <summary>
        ///// 删除区域
        ///// </summary>
        ///// <param name="areaMetadata"></param>
        //public void RemoveArea(Metadata.Area areaMetadata)
        //{
        //}

        //public void CreatePart(Metadata.Part part)
        //{
        //}

        //public void RemovePart(Metadata.Part part)
        //{
        //}

        /// <summary>
        /// 选中一个组件实例
        /// </summary>
        /// <param name="partInstance"></param>
        public void SelectPartInstance(Coolzon.UI.Part partInstance)
        {
            //long beginTicks = DateTime.Now.Ticks;
            for (int index = 0; index < _partInstanceSelectedEventReceiverList.Count; index++)
            {
                _partInstanceSelectedEventReceiverList[index].OnPartInstanceSelected(partInstance);
            }
            //Debug.WriteLine("ticks:{0}", DateTime.Now.Ticks - beginTicks);
        }

        /// <summary>
        /// 选择一个组件元数据，用于放置组件到试图上
        /// </summary>
        /// <param name="partMetadata"></param>
        public void SelectPartMetadata(Coolzon.Metadata.Part partMetadata)
        {
            for (int index = 0; index < _partMetadataSelectedEventReceiverList.Count; index++)
            {
                _partMetadataSelectedEventReceiverList[index].OnPartMetadataSelected(partMetadata);
            } 
        }

        public void SelectPartLibItem(Coolzon.Metadata.PartLibItem partLibItem)
        {
            for (int index = 0; index < this._partLibItemSelectedEventReceiverList.Count; index++)
            {
                _partLibItemSelectedEventReceiverList[index].OnPartLibItemSelected(partLibItem);
            }
        }

        /// <summary>
        /// 组件实例被创建成功
        /// </summary>
        /// <param name="parentPart"></param>
        /// <param name="createdPart"></param>
        public void PartInstanceCreated(Coolzon.UI.IPartContainer parentPart, Coolzon.UI.Part createdPart)
        {
            for (int index = 0; index < _solutionItemChangedEventReceiverList.Count; index++)
            {
                _solutionItemChangedEventReceiverList[index].OnPartInstanceCreated(parentPart,  createdPart);
            }
        }
    }
}
